/*
 * File:         MessageTest.java
 * Author:       Robert Bittle <guywithnose@gmail.com>
 */
package faceBook.tests;

import static org.junit.Assert.*;

import java.sql.Date;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.Iterator;

import org.junit.Test;

import tests.AppEngineTest;

import faceBook.Message;
import faceBook.User;
import faceBook.tests.helper.TestUsers;

/**
 * The Class MessageTest.
 */
@SuppressWarnings("static-method")
public class MessageTest extends AppEngineTest {

	/**
	 * Tests the getID method.
	 */
	@Test
	public void testGetID() {
		Message message = new Message(4);
		assertEquals(4, message.getID());
	}

  /**
   * Tests the getLink method.
   */
  @Test
  public void testGetLink() {
    Message message = new Message();
    message.setLink("abc");
    assertEquals("abc", message.getLink());
  }
  
  /**
   * Tests the getMessage method.
   */
  @Test
  public void testGetMessage() {
    Message message = new Message();
    message.setMessage("def");
    assertEquals("def", message.getMessage());//Verify the object is equal
  }
  
  /**
   * Tests the getSendDate method.
   */
  @Test
  public void testGetSendDate() {
    Message message = new Message();
    message.setSendDate(new Date(450000000000L));
    assertEquals(new Date(450000000000L).toString(), message.getSendDate().toString());//Verify the object is equal
  }
  
  /**
   * Tests the getUser method.
   */
  @Test
  public void testGetUser() {
    Message message = new Message();
    message.setUser(new User(123));
    assertEquals(123, message.getUser().getID());//Verify the object is equal
  }
  
  /**
   * Tests the getFriend method.
   */
  @Test
  public void testGetFriendList() {
    Message message = new Message();
    message.addFriend(new User(123));
    assertEquals(123, message.getFriendList().get(0).friend.getID());//Verify the object is equal
  }
  
  /**
   * Tests the getConfirmation method.
   */
  @Test
  public void testGetConfirmation() {
    Message message = new Message();
    User user = new User(123);
    message.addFriend(user);
    message.setConfirmation("confirmed", user);
    assertEquals("confirmed", message.getConfirmations());//Verify the object is equal
  }

  /**
   * Tests the getType method.
   */
  @Test
  public void testGetType() {
    Message message = new Message();
    message.setType("my type");
    assertEquals("my type", message.getType());//Verify the object is equal
  }
  
  /**
   * Tests the getType method.
   */
  @Test
  public void testGetHoliday() {
    Message message = new Message();
    message.setHoliday(32);
    assertEquals(32, message.getHoliday());//Verify the object is equal
  }
	
	/**
	 * Tests the dataBase method.
	 */
    @Test
	public void testDataBase() {
      User user1 = new User(123);
      User user2 = new User(789);
      user1.save();
      user2.save();
		Message message = new Message();
    message.setLink("abc");
    message.setMessage("def");
    message.setSendDate(new Date(450000000000L));
    User friend = new User(456);
    message.setUser(new User(123));
    message.addFriend(friend);
    message.setConfirmation("confirmed", friend);
    message.setType("my type");
    message.setHoliday(32);
    assertTrue(message.save());//Save the object
		Message message2 = new Message(message.getID());//Load the object
    assertEquals("abc", message2.getLink());//Verify the object is equal
    assertEquals("def", message2.getMessage());//Verify the object is equal
    assertEquals(new Date(450000000000L).toString(), message2.getSendDate().toString());//Verify the object is equal
    assertEquals(123, message2.getUser().getID());//Verify the object is equal
    assertEquals(456, message2.getFriendList().get(0).friend.getID());//Verify the object is equal
    assertEquals("confirmed", message2.getConfirmations());//Verify the object is equal
    assertEquals("my type", message2.getType());//Verify the object is equal
    assertEquals(32, message2.getHoliday());//Verify the object is equal
    message2.setLink("ghi");
    message2.setMessage("jkl");
    message2.setSendDate(new Date(400000000000L));
    message2.setUser(new User(789));
    User friend2 = new User(321);
    message2.addFriend(friend2);
    message2.setConfirmation("again", friend2);
    message2.setType("my new type");
    message2.setHoliday(23);
    assertTrue(message2.save());//Save the object
		Message message3 = new Message(message.getID());//Load the object again
    assertEquals("ghi", message3.getLink());//Verify the object is equal
    assertEquals("jkl", message3.getMessage());//Verify the object is equal
    assertEquals(new Date(400000000000L).toString(), message3.getSendDate().toString());//Verify the object is equal
    assertEquals(789, message3.getUser().getID());//Verify the object is equal
    assertEquals(321, message3.getFriendList().get(0).friend.getID());//Verify the object is equal
    assertEquals("again,confirmed", message3.getConfirmations());//Verify the object is equal
    assertEquals("my new type", message3.getType());//Verify the object is equal
    assertEquals(23, message3.getHoliday());//Verify the object is equal
		message.delete();//Delete the object
		Message message4 = new Message(message.getID());
    assertEquals(null, message4.getLink());//Verify the object is equal
    assertEquals(null, message4.getMessage());//Verify the object is equal
    assertEquals(null, message4.getSendDate());//Verify the object is equal
    assertEquals(null, message4.getUser());//Verify the object is equal
    assertEquals(0, message4.getFriendList().size());//Verify the object is equal
    assertEquals("", message4.getConfirmations());//Verify the object is equal
    assertEquals(null, message4.getType());//Verify the object is equal
    assertEquals(0, message4.getHoliday());//Verify the object is equal
    user1.delete();
    user2.delete();
	}

	/**
	 * Tests the toJSONString method.
	 */
	@Test
	public void testToJSONString() {
		Message message = new Message(123);
    message.setLink("abc");
    message.setMessage("def");
    message.setSendDate(new Date(450000000000L));
    message.setUser(new User(123));
    User friend = new User(456);
    message.addFriend(friend);
    message.setConfirmation("confirmed", friend);
    message.setType("my type");
    message.setHoliday(32);
		assertEquals("{\"holiday\":32,\"sendDate\":\"1984-04-05\",\"message\":\"def\",\"id\":123,\"isDefault\":false,\"link\":\"abc\",\"isSent\":true,\"type\":\"my type\",\"friendList\":[{\"confirmation\":\"confirmed\",\"friend\":{\"id\":456,\"useDefaultDB\":false,\"useReply\":false}}],\"user\":{\"id\":123,\"useDefaultDB\":false,\"useReply\":false}}", message.toJSONString());			
	}
  
  /**
   * Tests the postMessage method.
   */
  @Test
  public void testPostMessage() {
    Message message = new Message();
    message.setUser(TestUsers.getTestUser(0));
    message.addFriend(TestUsers.getTestUser(1));
    message.setMessage("Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.\n\nNow we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.\n\nBut, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth.");
    message.setLink("");
    assertEquals("", message.getConfirmations());
    message.postMessage();
    assertNotNull(message.getConfirmations());
    message.delete();
  }
  
  /**
   * Tests the getAllMessageForDay method.
   */
  @Test
  public void testGetAllMessageForDay() {
    User user = new User(123);
    user.save();
    Message message = new Message();
    message.setUser(user);
    message.setSendDate(new Date(650000000000L));
    assertTrue(message.save());
    Message message2 = new Message();
    message2.setUser(user);
    message2.setSendDate(new Date(650090000000L));
    assertTrue(message2.save());
    Message message3 = new Message();
    message3.setUser(user);
    message3.setSendDate(new Date(650200000000L));
    assertTrue(message3.save());
    ArrayList<Message> Messages = Message.getAllMessageForDay(new GregorianCalendar(1990, 7, 8));
    message.delete();
    message2.delete();
    message3.delete();
    assertTrue(Messages.size() > 0);
    Iterator<Message> i = Messages.iterator();
    while (i.hasNext())
    {
      Message m = i.next();
      assertEquals("1990-08-08", m.getSendDate().toString());
    }
    user.delete();
  }
    
  /**
   * Tests the getAllMessagesForUser method.
   */
  @Test
  public void testGetAllMessagesForUser() {
    User user = new User(122);
    user.save();
    Message message = new Message();
    message.setUser(user);
    message.save();
    User user2 = new User(123);
    user2.save();
    Message message2 = new Message();
    message2.setUser(user2);
    message2.save();
    User user3 = new User(124);
    user3.save();
    Message message3 = new Message();
    message3.setUser(user3);
    message3.save();
    ArrayList<Message> Messages = Message.getAllMessagesForUser(new User(123));
    message.delete();
    message2.delete();
    message3.delete();
    user.delete();
    user2.delete();
    user3.delete();
    assertTrue(Messages.size() > 0);
    for(Message m : Messages)
    {
      assertEquals(123, m.getUser().getID());
    }
  }

}
